// pages/exchange/exchange.js
var common = require("../../utils/Conmon.js");
var appData = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
     index:0,
     Bilvs: [],
     arrayObj: [],
     exchangeList: [],
     Type:'1',
     kindName:'佣金',
     CssType: 'btn-custom',
     Money:'',
     liveMoney:0, //可兑换的金额
  },

  /**
   * 绑定选择事件
   */
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value);
    var that = this;
    that.setData({
      index: e.detail.value,
      CssType: 'btn-custom',
      Money: ''
    })
  },

  /**
   * 绑定输入兑换金额事件
   */
  bindInputMoney:function(e){
    var that = this;
    var money = e.detail.value;
    var css = 'btn-primary';
    if (Number(money) < 0 || Number(that.data.liveMoney) <= 0 || money=="") {
      css = 'btn-custom';
      money = '';
    }
    if (Number(money) > Number(that.data.liveMoney)){
      money = that.data.liveMoney;
    }
    that.setData({
      CssType: css,
      Money: money
    })
  },
 /**
  * 绑定确定兑换
  */
  bindSureExchange:function(e){
    var that=this;
    if (that.data.CssType == 'btn-custom') {
      return;
    }
    if (Number(that.data.Money) > Number(that.data.liveMoney))
    {
      wx.showModal({ title: '警告', content: "可用" + that.data.kindName+"不足兑换", showCancel: false })
      return;
    }
    
    //开始兑换
    var exchangeId = that.data.arrayObj[that.data.index].SetId;
    wx.showLoading({ title: '兑换中...', mask: true })
    wx.request({
      url: appData.apiurl + 'member/api/Exchangere/SoureExchange',
      method: 'GET',
      data: {custId: appData.custID, userId: wx.getStorageSync('UserInfo').UserID, exchangeNum: that.data.Money, setId: exchangeId},
      header: {
        "content-type": "application/json",
        'X_ManuName': appData.manuName
      },
      success: function (res) {
        wx.hideLoading()
        if (res.statusCode == 200 && res.data.code > 0) {
          if (res.data.content.Item1 > 0) {
            wx.navigateTo({
              url: "/pages/exchangeSucceed/exchangeSucceed?type=" + that.data.Type + "&name=" + that.data.kindName + "&value=" + res.data.content.Item2 + "&feeValue=" + res.data.content.Item3
            })

          } else if (res.data.content.Item1 == 0) {
            wx.showModal({ title: '错误', content: "兑换数量需要大于0", showCancel: false })
          } else if (res.data.content.Item1 == -1) {
            wx.showModal({ title: '错误', content: that.data.kindName+"不足", showCancel: false })
          } else if (res.data.content.Item1 == -2) {
            wx.showModal({ title: '错误', content: "单次最低兑换数小于‘" + res.data.content.Item3 + "’", showCancel: false })
          } else if (res.data.content.Item1 == -3) {
            wx.showModal({ title: '错误', content: "日最高兑换数大于‘" + res.data.content.Item3 + "’", showCancel: false })
          } else if (res.data.content.Item1 == -4) {
            wx.showModal({ title: '错误', content: "周最高兑换数大于‘" + res.data.content.Item3 + "’", showCancel: false })
          } else if (res.data.content.Item1 == -5) {
            wx.showModal({ title: '错误', content: "月最高兑换数大于‘" + res.data.content.Item3 + "’", showCancel: false })
          } else if (res.data.content.Item1 == -10) {
            wx.showModal({ title: '错误', content: '兑换失败', showCancel: false })
          }
        }
      },
      fail: function (res) {
        wx.hideLoading()
        wx.showModal({ title: '错误', content: '请求兑换异常错误', showCancel: false })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    common.pageInit( function () { });
    var that=this;
    var exchangeType = options.type; //兑换类型
    var exchangeName = options.name; //兑换名称
    if (exchangeType != null && exchangeType!=undefined){
      that.setData({
        kindName: exchangeName,
        Type: exchangeType
      })
    }

    that.GetliveExchangeAmount(exchangeType);
    that.GetExchangeType(exchangeType);
  },

/**
 * 获取可兑换的币种数量
 */
  GetliveExchangeAmount: function (_type){
    var that = this;
    wx.request({
      url: appData.apiurl + 'member/api/currencyinfo/getuserdifference',
      method: 'GET',
      data: { custid: appData.custID, userId: wx.getStorageSync('UserInfo').UserID, type: _type == null ? 1 : _type },
      header: {
        "content-type": "application/json",
        'X_ManuName': appData.manuName
      },
      success: function (res) {
        if (res.statusCode == 200 && res.data.code>0) {
          if (res.data.content != null) {
            var money = res.data.content.LiveAmount;
            that.setData({
              liveMoney: money
            });
          }
        }
      },
      fail: function (res) {
        wx.showModal({ title: '错误', content: '请求获取异常错误', showCancel: false })
        console.log(res.data)
      }
    })
  },
  
  /**
   * 获取可兑换的类型
   */
  GetExchangeType: function (_type){
    var that = this;
    wx.showLoading({ title: '加载中', mask: true })
    wx.request({
      url: appData.apiurl + 'member/api/Exchangere/GetExchangereSetList',
      method: 'GET',
      data: { custId: appData.custID, currId: _type },
      header: {
        "content-type": "application/json",
        'X_ManuName': appData.manuName
      },
      success: function (res) {
        wx.hideLoading();
        var len = that.data.exchangeList;
        var exChangelen = that.data.arrayObj;
        var newbilvs = that.data.Bilvs;
        if (res.statusCode == 200 && res.data.code>0) {
          if (res.data.content != null) {
            for (var bi in res.data.content) {
              var arr = {};
              len.push(res.data.content[bi])

              if (res.data.content.hasOwnProperty(bi)) {
                if (_type != res.data.content[bi].ExchangeKind) {
                  if (res.data.content[bi].ExchangeKind == 1) {    //佣金
                    arr.icon = "dh_icon02";
                  }
                  else if (res.data.content[bi].ExchangeKind == 2) { //积分
                    arr.icon = "dh_icon03";
                  }
                  else if (res.data.content[bi].ExchangeKind == 3) { //代金券
                    arr.icon = "dh_icon01";
                  }
                  else if (res.data.content[bi].ExchangeKind == 4) { //卡券
                    arr.icon = "dh_icon01";
                  }
                  else if (res.data.content[bi].ExchangeKind == 5) { //充值币
                    arr.icon = "dh_icon01";
                  }
                  else if (res.data.content[bi].ExchangeKind == 6) { //余额
                    arr.icon = "dh_icon01";
                  }
                  arr.name = res.data.content[bi].ExchangeValue;
                  arr.SetId = res.data.content[bi].ID;
                  arr.oneTiMoney = res.data.content[bi].OneTiMoney;
                  arr.id = "duihuan" + res.data.content[bi].ExchangeKind;
                  if (res.data.content[bi].IsOpenFee != 1)
                    arr.feeBili = "免费"
                  else
                    arr.feeBili = res.data.content[bi].CommChargeBi + "%";

                  newbilvs.push(arr.feeBili)

                  //兑换类型添加到集合中
                  exChangelen.push(arr);
                }
              }
            }
            that.setData({
              Bilvs: newbilvs,
              arrayObj: exChangelen,
              exchangeList: len
            });
          }
        }
      },
      fail: function (res) {
        wx.hideLoading()
        wx.showModal({ title: '错误', content: '请求获取异常错误', showCancel: false })
        console.log(res.data)
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})